package com.ptu.mapper;

import com.ptu.entity.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressMapper {

    @Select("select * from address where user_id = #{id}")
    List<Address> list(Integer id);

    @Insert("insert into address(user_id, recipient, gender, phone, province, city, district, detailed_address, is_default , create_time, update_time) " +
            "values(#{userId}, #{recipient}, #{gender}, #{phone}, #{province}, #{city}, #{district}, #{detailedAddress}, #{isDefault}, NOW(), NOW())"
    )
    void add(Address address);

    @Delete("delete from address where id = #{id}")
    void delete(Integer id);

    @Update("update address set recipient = #{recipient}, gender = #{gender}, phone = #{phone}, province = #{province}, city = #{city}, district = #{district}, detailed_address = #{detailedAddress}, update_time = NOW() where id = #{id} and user_id = #{userId}")
    void update(Address address);
    
    @Update("update address set is_default = 1 where user_id = #{userId} and id = #{id}")
    void setDefault(Integer id, Integer userId);

    @Select("select id from address where user_id = #{userId} and is_default = 1")
    Integer getDefaultId(Integer userId);

    @Update("update address set is_default = 0 where id = #{oldId} and user_id = #{userId}")
    void deleteDefault(Integer oldId, Integer userId);

    @Select("select * from address where user_id = #{userId} and is_default = 1")
    Address getDefault(Integer userId);

    @Delete("delete from address where user_id = #{userId}")
    void deleteByUserId(Integer userId);

    @Select("SELECT id, user_id, recipient, gender, phone, province, city, district, detailed_address, is_default, create_time, update_time " +
            "FROM address " +
            "WHERE id = #{addressId}")
    Address selectAddressById(Long addressId);
}
